package cxbks.java.controller;

import cxbks.java.core.AjaxResult;
import cxbks.java.dao.entity.User;
import cxbks.java.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:
 * @CreateTime: 2021-09-14 16:23
 * @Description:
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping(value = "/findAll")
	public AjaxResult findAll() {
		return AjaxResult.success(userService.findAll());
	}

	@GetMapping(value = "/findPageUser")
	public AjaxResult findPageUser(@RequestParam(name = "begin") int begin, @RequestParam(name = "end") int end) {
		return AjaxResult.success(userService.findPageUser(begin, end));
	}

	@GetMapping(value = "/findLike")
	public AjaxResult findLike(@RequestParam(name = "nickName") String nickName) {
		return AjaxResult.success(userService.findByNickNameLike(nickName));
	}

	@PostMapping(value = "/saveAll")
	public AjaxResult saveAll(@RequestBody List<User> userList) {
		return AjaxResult.success(userService.saveAll(userList));
	}

	@PostMapping(value = "/save")
	public AjaxResult save(@RequestBody User user) {
		return AjaxResult.success(userService.save(user));
	}

	@PutMapping(value = "/delete/{id}")
	public AjaxResult delete(@PathVariable String id) {
		return AjaxResult.success(userService.delete(id));
	}

	@PutMapping(value = "deleteAll")
	public AjaxResult deleteAll() {
		return AjaxResult.success(userService.deleteAll());
	}
}
